//
//  ClassOfGoodsVC.m
//  EJLX
//
//  Created by adc on 15/11/11.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "ClassOfGoodsVC.h"
#import "GoodsClassCell.h"
#import "GoodsInfoVC.h"
#import "PinPaiViewController.h"
#import "ShoppingOfCartVC.h"
#import "ShangChengFanLiViewController.h"//
@interface ClassOfGoodsVC ()

@end

@implementation ClassOfGoodsVC

-(void)viewWillAppear:(BOOL)animated{
  
    [HttpsManager classBtnBlock:^(NSMutableArray *array) {
          [GMDCircleLoader setOnView:self.view withTitle:nil animated:YES];
        NSLog(@"arr %@",array);
        if (array == nil) {
            LoginVC *vc = [[LoginVC alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            self.btnArr = array;
            [self addBtn];
            [GMDCircleLoader hideFromView:self.view animated:YES];
        }
    }];
    

    
    
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    

    self.view.backgroundColor = [UIColor whiteColor];
    [HttpsManager classOfGoodswithCategoryID:1 completionBlock:^(NSMutableArray *arr) {
        self.array = arr;
        
        [collect reloadData];
    }];

    _bigView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, WIDE, HEIGHT - 64 -56 )];
    _bigView.backgroundColor = RGB(240, 240, 240);
    [self.view addSubview:_bigView];

    [self addSearchView];
    [self creatNavAndTabbar];
    [self addContentView];
}

-(void)creatNavAndTabbar{
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, WIDE, 44)];
    navView.backgroundColor = RGB_COLOR(0x00c49f);
    [self.view addSubview:navView];
    
  
    
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(10, 7, 30, 30);
    leftBtn.layer.cornerRadius = 20;
    [leftBtn setImage:[UIImage imageNamed:@"返回箭头"] forState:UIControlStateNormal] ;
    [leftBtn addTarget:self action:@selector(backBtn) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:leftBtn];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(WIDE/2-60, 7, 120, 30  )];
    title.text = @"品牌分销";
    title.textAlignment = NSTextAlignmentCenter;
    title.textColor = [UIColor whiteColor];
    [navView addSubview:title];
    
    //tabbar自定义还是系统的
    UIView *tabbarView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT-56,WIDE, 56)];
    tabbarView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:tabbarView];
    
    UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDE, 1)];
    l.backgroundColor = RGB(240, 240, 240);
    
    
    
    [tabbarView addSubview:l];
    NSArray *tabTitleArr = @[@"首页",@"分类",@"购物车"];
    NSArray *tabImageArr = @[@"首页灰",@"分类绿",@"购物车"];
    for (int i =0 ; i<3; i++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom] ;
        btn.frame = CGRectMake(WIDE/3.f*i, 0, WIDE/3.f, 56);
        [tabbarView addSubview:btn];
        UIButton *tabbarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        tabbarBtn.frame = CGRectMake((WIDE-25*3)/6.f+(25+(WIDE-25*3)/3.f)*i, 5, 25, 20);
        // tabbarBtn.layer.cornerRadius = 20;
        btn.tag = 103+i;
        
        [tabbarBtn setImage:[UIImage imageNamed:tabImageArr[i]] forState:UIControlStateNormal] ;
        
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(((float)WIDE/3)*i+10, 30,((float)WIDE/3)-20, 20)];
        lb.text = tabTitleArr[i];
        if (i == 1) {
            lb.textColor = RGB_COLOR(0x00c49f);
        }
        
        lb.textAlignment = NSTextAlignmentCenter;
        lb.font = [UIFont systemFontOfSize:15.0f];
        [tabbarView addSubview:lb];
        [tabbarView addSubview:tabbarBtn];
    }
}
-(void)backBtn{
     [self.navigationController popViewControllerAnimated:YES];
    
}
-(void)btnClick:(UIButton *)btn{
    if (btn.tag==101) {
        [self.navigationController popViewControllerAnimated:YES];
    }else if (btn.tag ==103){
       [self.navigationController popViewControllerAnimated:YES];
    
    }else if (btn.tag ==1.4){
        
    }else{
        //gouwuchew
        ShoppingOfCartVC *cart = [[ShoppingOfCartVC alloc] init];
        [self.navigationController pushViewController:cart animated:YES];
    }
    
}
-(void)addSearchView{
    UISearchBar *search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, WIDE, 44) ];
    search.delegate = self;
    // search.showsCancelButton = YES;
    // search.showsSearchResultsButton =YES;
    search.showsScopeBar = YES;
    // [search setShowsCancelButton:YES animated:YES];
    
    search.backgroundColor = [UIColor whiteColor];
    // search.layer.cornerRadius = 11;
    // search.layer.masksToBounds = YES;
    
    //    [search.layer setBorderColor:RGB_COLOR(0x00c49f).CGColor];
    //    [search.layer setBorderWidth:1];
    search.placeholder = @"搜索商品";
    // search.scopeButtonTitles = @[@"123"];
    [_bigView addSubview:search];
    UIButton*but=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, WIDE, 44)];
    [but addTarget:self action:@selector(searchBut) forControlEvents:UIControlEventTouchUpInside];
    [_bigView addSubview:but];

}
-(void)searchBut
{
    SearchViewController*VC=[[SearchViewController alloc]init];
    NSLog(@"search");
    [self.navigationController pushViewController:VC animated:YES];
    

}

#pragma mark -addContent

-(void)addBtn{
    for (int i = 0; i<self.btnArr.count; i++) {
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        leftBtn.frame = CGRectMake(0, 44+60*i+5, 80, 60);
        leftBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        leftBtn.titleLabel.font = [UIFont boldSystemFontOfSize:15.f];
        leftBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
        [leftBtn setBackgroundColor:RGB(240, 240, 246) forState:UIControlStateNormal];
        [leftBtn setBackgroundColor:[UIColor whiteColor] forState:UIControlStateSelected];
    
        [leftBtn setTitle:self.btnArr[i] forState:UIControlStateNormal];
        [leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
     
        [leftBtn setBackgroundImage:[UIImage imageNamed:@"非点击.jpg"] forState:UIControlStateNormal];
      //  [leftBtn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [leftBtn setBackgroundImage:[UIImage imageNamed:@"点击.jpg"] forState:UIControlStateSelected];
        leftBtn.tag = 100+ i;
        if (i == 0) {
            _beforeBtn = leftBtn;
            _beforeBtn.selected = YES;
            
        }
        [leftBtn addTarget:self action:@selector(classBtn:) forControlEvents:UIControlEventTouchUpInside];
        [_bigView addSubview:leftBtn];
    }

}
-(void)addContentView{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;//竖直滚动
    // layout.sectionInset = UIEdgeInsetsMake(100, 0, 20, 0);
    
    collect = [[UICollectionView alloc] initWithFrame:CGRectMake(83, 44, WIDE-83, HEIGHT-56-64-44) collectionViewLayout:layout];
    collect.dataSource = self;
    collect.delegate = self;
    collect.backgroundColor = [UIColor whiteColor];;
    
    [collect registerNib:[UINib nibWithNibName:@"GoodsClassCell" bundle:nil] forCellWithReuseIdentifier:@"collect"];
    [_bigView addSubview:collect];
    
    
    
}
-(void)classBtn:(UIButton *)btn{
  
    _beforeBtn.selected = NO;
    btn.selected = YES;
    _beforeBtn = btn;
    
    int a =(int)btn.tag +1-100;
    
    
    JGProgressHUD *HUD = [[JGProgressHUD alloc] initWithStyle:JGProgressHUDStyleDark];
    HUD.textLabel.text = @"正在加载中";
    
    HUD.useProgressIndicatorView = YES;
 
    [HUD showInView:self.navigationController.view];
    
    
    
    //[GMDCircleLoader setOnView: self.view  withTitle:nil animated:YES];
  [HttpsManager classOfGoodswithCategoryID:a completionBlock:^(NSMutableArray *arr) {
                self.array = arr;
     // [GMDCircleLoader hideFromView:self.view animated:YES];
                [collect reloadData];
      [HUD dismissAfterDelay:0.1];

      
            }];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.array.count;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
   GoodsClassCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collect" forIndexPath:indexPath];
        if (!cell) {
            cell = [[NSBundle mainBundle] loadNibNamed:@"MyShopCell" owner:self options:nil][0];
        }
    ClassOfGoods *good = [self.array objectAtIndex:indexPath.row];
    
    cell.backgroundColor = [UIColor whiteColor];
    [cell.classForGoodsImage sd_setImageWithURL:[NSURL URLWithString:good.avatar] placeholderImage:[UIImage imageNamed:@"默认图片.jpg"] ];
    cell.classForGoodsName.text = good.name;
    cell.classForGoodsName.adjustsFontSizeToFitWidth = YES;
    cell.classForGoodsName.textAlignment = NSTextAlignmentCenter;
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ClassOfGoods *good = [self.array objectAtIndex:indexPath.row];
    ShangChengFanLiViewController*sC=[[ShangChengFanLiViewController alloc]init];
    sC.subcategoryID = [NSString stringWithFormat:@"%d", good.subId];
    sC.titleName = good.name;
    [self.navigationController pushViewController:sC animated:YES];
    
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((CGFloat)(WIDE - 80-18)/3.1f, (CGFloat)1.2* (WIDE - 80-18)/3.f);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 2, 0, 0);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
